﻿namespace Pub.Class
{
    using System;
    using System.Drawing;
    using System.Drawing.Drawing2D;
    using System.Drawing.Imaging;
    using System.IO;
    using System.Runtime.InteropServices;

    public class CutImage
    {
        private string errMSG;
        private int height;
        private int size;
        private int width;

        public bool CutImageByHeight(string imagePath, string savePath, int square)
        {
            try
            {
                int num = square;
                Image image = Image.FromFile(imagePath);
                float width = image.Width;
                float height = image.Height;
                this.height = num;
                this.width = (int) ((num * width) / height);
                Image image2 = new Bitmap(image.Width, image.Height, PixelFormat.Format32bppRgb);
                Graphics graphics = Graphics.FromImage(image2);
                graphics.DrawImage(image, 0, 0, image.Width, image.Height);
                graphics.Dispose();
                Image image3 = image2.GetThumbnailImage(this.width, this.height, null, IntPtr.Zero);
                image3.Save(savePath, image.RawFormat);
                image.Dispose();
                image2.Dispose();
                image3.Dispose();
                FileStream stream = new FileStream(savePath, FileMode.Open, FileAccess.Read);
                this.size = (int) stream.Length;
                stream.Close();
                return true;
            }
            catch (Exception exception)
            {
                this.errMSG = exception.ToString();
                return false;
            }
        }

        public bool CutImageByWidth(string imagePath, string savePath, int square)
        {
            try
            {
                int num = square;
                Image image = Image.FromFile(imagePath);
                float width = image.Width;
                float height = image.Height;
                this.width = num;
                this.height = (int) ((num * height) / width);
                Image image2 = new Bitmap(image.Width, image.Height, PixelFormat.Format32bppRgb);
                Graphics graphics = Graphics.FromImage(image2);
                graphics.DrawImage(image, 0, 0, image.Width, image.Height);
                graphics.Dispose();
                Image image3 = image2.GetThumbnailImage(this.width, this.height, null, IntPtr.Zero);
                image3.Save(savePath, image.RawFormat);
                image.Dispose();
                image2.Dispose();
                image3.Dispose();
                FileStream stream = new FileStream(savePath, FileMode.Open, FileAccess.Read);
                this.size = (int) stream.Length;
                stream.Close();
                return true;
            }
            catch (Exception exception)
            {
                this.errMSG = exception.ToString();
                return false;
            }
        }

        public bool CutImageByWidthHeight(string imagePath, string savePath, int newWidth, int newHeight)
        {
            bool flag;
            Image image = Image.FromFile(imagePath);
            int width = image.Width;
            int height = image.Height;
            System.Drawing.Size size = ResizeSite(new System.Drawing.Size(newWidth, newHeight), new System.Drawing.Size(width, height));
            int num3 = size.Width;
            int num4 = size.Height;
            int x = 0;
            int y = 0;
            x = (newWidth - num3) / 2;
            y = (newHeight - num4) / 2;
            Image image2 = new Bitmap(num3, num4);
            Graphics graphics = Graphics.FromImage(image2);
            graphics.InterpolationMode = InterpolationMode.HighQualityBicubic;
            graphics.SmoothingMode = SmoothingMode.HighQuality;
            graphics.Clear(Color.Transparent);
            graphics.DrawImage(image, new Rectangle(0, 0, num3, num4), new Rectangle(0, 0, width, height), GraphicsUnit.Pixel);
            Image image3 = new Bitmap(newWidth, newHeight);
            Graphics graphics2 = Graphics.FromImage(image3);
            graphics2.InterpolationMode = InterpolationMode.HighQualityBicubic;
            graphics2.SmoothingMode = SmoothingMode.HighQuality;
            graphics2.Clear(Color.White);
            graphics2.DrawImageUnscaled(image2, new Point(x, y));
            try
            {
                image3.Save(savePath, image.RawFormat);
                flag = true;
            }
            catch (Exception exception)
            {
                this.errMSG = exception.ToString();
                flag = false;
            }
            finally
            {
                image.Dispose();
                image2.Dispose();
                image3.Dispose();
                graphics2.Dispose();
                graphics.Dispose();
            }
            return flag;
        }

        public bool CutImageCustom(string imagePath, string savePath, int cutWidth, int cutHeight)
        {
            try
            {
                Image image = Image.FromFile(imagePath);
                float width = image.Width;
                float height = image.Height;
                float num3 = width / ((float) cutWidth);
                float num4 = height / ((float) cutHeight);
                if (num3 < num4)
                {
                    this.width = (int) ((width * cutHeight) / height);
                    this.height = cutHeight;
                }
                else
                {
                    this.width = cutWidth;
                    this.height = (int) ((cutWidth * height) / width);
                }
                Bitmap bitmap = new Bitmap(this.width, this.height, PixelFormat.Format32bppRgb);
                bitmap.SetResolution(72f, 72f);
                Graphics graphics = Graphics.FromImage(bitmap);
                graphics.CompositingQuality = CompositingQuality.HighQuality;
                graphics.SmoothingMode = SmoothingMode.HighQuality;
                graphics.InterpolationMode = InterpolationMode.HighQualityBicubic;
                graphics.PixelOffsetMode = PixelOffsetMode.HighQuality;
                graphics.FillRectangle(new SolidBrush(Color.White), 0, 0, this.width, this.height);
                Rectangle destRect = new Rectangle(0, 0, this.width, this.height);
                graphics.DrawImage(image, destRect, 0, 0, image.Width, image.Height, GraphicsUnit.Pixel);
                graphics.Dispose();
                EncoderParameters encoderParams = new EncoderParameters(1);
                encoderParams.Param[0] = new EncoderParameter(Encoder.Quality, 100L);
                ImageCodecInfo[] imageEncoders = ImageCodecInfo.GetImageEncoders();
                ImageCodecInfo info = null;
                foreach (ImageCodecInfo info2 in imageEncoders)
                {
                    if (info2.MimeType == "image/jpeg")
                    {
                        info = info2;
                    }
                }
                if (info.IsNotNull())
                {
                    bitmap.Save(savePath, info, encoderParams);
                }
                else
                {
                    bitmap.Save(savePath, image.RawFormat);
                }
                image.Dispose();
                bitmap.Dispose();
                FileStream stream = new FileStream(savePath, FileMode.Open, FileAccess.Read);
                this.size = (int) stream.Length;
                stream.Close();
                return true;
            }
            catch (Exception exception)
            {
                this.errMSG = exception.ToString();
                return false;
            }
        }

        public bool CutImageCustomMin(string imagePath, string savePath, int cutWidth, int cutHeight)
        {
            try
            {
                Image image = Image.FromFile(imagePath);
                float width = image.Width;
                float height = image.Height;
                float num3 = width / ((float) cutWidth);
                float num4 = height / ((float) cutHeight);
                if (num3 < num4)
                {
                    this.width = (int) ((width * cutHeight) / height);
                    this.height = cutHeight;
                }
                else
                {
                    this.width = cutWidth;
                    this.height = (int) ((cutWidth * height) / width);
                }
                Image image2 = new Bitmap(image.Width, image.Height, PixelFormat.Format32bppRgb);
                Graphics graphics = Graphics.FromImage(image2);
                graphics.DrawImage(image, 0, 0, image.Width, image.Height);
                graphics.Dispose();
                Image image3 = image2.GetThumbnailImage(this.width, this.height, null, IntPtr.Zero);
                image3.Save(savePath, image.RawFormat);
                image.Dispose();
                image2.Dispose();
                image3.Dispose();
                FileStream stream = new FileStream(savePath, FileMode.Open, FileAccess.Read);
                this.size = (int) stream.Length;
                stream.Close();
                return true;
            }
            catch (Exception exception)
            {
                this.errMSG = exception.ToString();
                return false;
            }
        }

        public bool CutImageSquare(string imagePath, string savePath, int square)
        {
            try
            {
                this.width = square;
                this.height = square;
                int width = square;
                int height = square;
                Image image = Image.FromFile(imagePath);
                if (image.Width >= image.Height)
                {
                    width = image.Height;
                    height = image.Height;
                }
                else
                {
                    width = image.Width;
                    height = image.Width;
                }
                Image image2 = new Bitmap(width, height, PixelFormat.Format32bppRgb);
                Graphics graphics = Graphics.FromImage(image2);
                graphics.InterpolationMode = InterpolationMode.High;
                graphics.SmoothingMode = SmoothingMode.HighQuality;
                Rectangle destRect = new Rectangle(0, 0, width, height);
                Rectangle srcRect = new Rectangle(0, 0, width, height);
                GraphicsUnit pixel = GraphicsUnit.Pixel;
                graphics.DrawImage(image, destRect, srcRect, pixel);
                graphics.Dispose();
                Image image3 = image2.GetThumbnailImage(this.width, this.height, null, IntPtr.Zero);
                image3.Save(savePath, image.RawFormat);
                image.Dispose();
                image2.Dispose();
                image3.Dispose();
                FileStream stream = new FileStream(savePath, FileMode.Open, FileAccess.Read);
                this.size = (int) stream.Length;
                stream.Close();
                return true;
            }
            catch (Exception exception)
            {
                this.errMSG = exception.ToString();
                return false;
            }
        }

        public bool GetImage(string imagePath)
        {
            try
            {
                Image image = Image.FromFile(imagePath);
                this.width = image.Width;
                this.height = image.Height;
                image.Dispose();
                FileStream stream = new FileStream(imagePath, FileMode.Open, FileAccess.Read);
                this.size = (int) stream.Length;
                stream.Close();
                return true;
            }
            catch (Exception exception)
            {
                this.errMSG = exception.ToString();
                return false;
            }
        }

        private static void GetProperSize(int trueWidth, int trueHeight, int placeWidth, int placeHeight, out int showWidth, out int showHeight)
        {
            if ((trueHeight < placeHeight) && (trueWidth < placeWidth))
            {
                showHeight = trueHeight;
                showWidth = trueWidth;
            }
            else
            {
                float num = trueWidth;
                float num2 = trueHeight;
                float num3 = num / ((float) placeWidth);
                float num4 = num2 / ((float) placeHeight);
                if (num3 < num4)
                {
                    showWidth = (int) ((num * placeHeight) / num2);
                    showHeight = placeHeight;
                }
                else
                {
                    showWidth = placeWidth;
                    showHeight = (int) ((placeWidth * num2) / num);
                }
            }
        }

        private static System.Drawing.Size ResizeSite(System.Drawing.Size specifySize, System.Drawing.Size originalSize)
        {
            System.Drawing.Size size = new System.Drawing.Size();
            float single1 = ((float) specifySize.Width) / ((float) specifySize.Height);
            float num = ((float) originalSize.Width) / ((float) originalSize.Height);
            if (specifySize.Width >= originalSize.Width)
            {
                size.Height = specifySize.Height;
                size.Width = (int) (size.Height * num);
                if (size.Width > specifySize.Width)
                {
                    size.Width = specifySize.Width;
                    size.Height = (int) (((float) size.Width) / num);
                }
                return size;
            }
            size.Width = specifySize.Width;
            size.Height = (int) (((float) size.Width) / num);
            if (size.Height > specifySize.Height)
            {
                size.Height = specifySize.Height;
                size.Width = (int) (size.Height * num);
            }
            return size;
        }

        public string ErrMSG
        {
            get
            {
                return this.errMSG;
            }
            set
            {
                this.errMSG = value;
            }
        }

        public int Height
        {
            get
            {
                return this.height;
            }
            set
            {
                this.Height = value;
            }
        }

        public int Size
        {
            get
            {
                return this.size;
            }
            set
            {
                this.size = value;
            }
        }

        public int Width
        {
            get
            {
                return this.width;
            }
            set
            {
                this.width = value;
            }
        }
    }
}

